package org.aspectsense.middleware.monitor.gui;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JFrame;

import org.aspectsense.middleware.ComponentContainer;
import org.aspectsense.middleware.ServiceWrapper;

public class ControlPanel {

	private static JFrame mf;
	public ControlPanel () {
		mf = new MainFrame();
		mf.setVisible(true);
	}

	/**
	 * Updates the GUI Tree that shows the current availability in Components and Services
	 * @param components2Containers
	 * @param services2Wrappers
	 */
	public static void updateTree(
			HashMap<String, ComponentContainer> components2Containers,
			HashMap<String, LinkedList<ServiceWrapper>> services2Wrappers) {
	
		int nodeCnt=0;
		ComponentTreeNode compTreeNode;
		ServiceTreeNode servTreeNode;
		Collection<LinkedList<ServiceWrapper>> wrapperLists;
		Iterator<LinkedList<ServiceWrapper>> itr;
		LinkedList<ServiceWrapper> wrapperList;
		
		HashMap<String, ITreeNode> treeNodes = new HashMap<String, ITreeNode>();
		
		if (components2Containers.isEmpty()&&services2Wrappers.isEmpty())
			return;
		
		if (!components2Containers.isEmpty())
		{
						
			Collection<ComponentContainer> x = components2Containers.values();
			Iterator<ComponentContainer> i = x.iterator();
			
			ComponentContainer cc;
		
			while (i.hasNext()){
				cc = i.next();
				
				compTreeNode = new ComponentTreeNode(cc, "Component");
				compTreeNode.setInfo(cc.getInfo());
				treeNodes.put("component"+nodeCnt, compTreeNode);
				
				nodeCnt++;
			}
		}
		if (!services2Wrappers.isEmpty())
		{
			
			wrapperLists = services2Wrappers.values();
			itr = wrapperLists.iterator();

			while(itr.hasNext())
			{
				wrapperList = (LinkedList<ServiceWrapper>) itr.next();
				for (ServiceWrapper sw:wrapperList)
				{
					//System.out.println (sw.getServiceName());
					servTreeNode = new ServiceTreeNode(sw, "Service");
					treeNodes.put("service"+nodeCnt, servTreeNode);
					
					nodeCnt++;
				}
			}
		}
		
		((MainFrame) mf).updateTree(treeNodes);
	}

}
